﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class Employee_Rent_ConfirmBooking : System.Web.UI.Page
{
    DBAcess db=new DBAcess();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Confirm | Nexus-Z Auto Rental";
        Button2.CausesValidation = false;
        Label1.Text = Session["name"].ToString();
        CarID.Text = Session["carID"].ToString();
        PlateNumber.Text = Session["plateNumber"].ToString();
        Color.Text = Session["color"].ToString();
        Brand.Text = Session["brand"].ToString();
        Make.Text = Session["make"].ToString();
        Speed.Text = Session["speed"].ToString();
        InsurancType.Text = Session["insuranceType"].ToString();
        InsuranceCost.Text = Session["insuranceCost"].ToString();
        LicenseNo.Text = Session["licenseNumber"].ToString();
        StartDate.Text = Session["startDate"].ToString();
        ReturnDate.Text = Session["returnDate"].ToString();
        TotalAmount.Text = Session["totalAmount"].ToString();
        Tariff.Text = Session["tariff"].ToString();
        if (Session["role"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }

    }
    protected void Confirm_Click(object sender, EventArgs e)
    {
        MySqlCommand cmd = new MySqlCommand("INSERT INTO booking(car_id, license_no, start_date, return_date, total_amout, emp_id, insurance_id, status) VALUES(" + int.Parse(CarID.Text) + ",'" + LicenseNo.Text + "','" + StartDate.Text + "','" + ReturnDate.Text + "'," + float.Parse(TotalAmount.Text) + "," + int.Parse(Session["employeeID"].ToString()) + "," + int.Parse(Session["insuranceID"].ToString()) + "," + 1 + ")", db.returnCon());
        db.open();
        int result=cmd.ExecuteNonQuery();
        if(result==-1){
            Label2.Text="cannot insert";
        }
        else{
            Label2.Text="can now take the car with plate no "+Session["plateNumber"].ToString();
        }
        db.close();
        Confirm.Visible = false;
        changeCarAvailability();
    }
    protected void changeCarAvailability()
    {
        MySqlCommand cmd=new MySqlCommand("update car set availability=0 where car_id="+int.Parse(CarID.Text)+";",db.returnCon());
        db.open();
        cmd.ExecuteNonQuery();
        db.close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Employee/Rent/RentCar.aspx");
    }
}